package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.entity.UmsRole;
import com.powernode.mapper.UmsRoleMapper;
import com.powernode.service.UmsRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author lxl
 * @since 2022-03-30
 */
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements UmsRoleService {
public IPage<UmsRole> list(Integer pageNo, Integer pageSize, String value) {

        //查询
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<UmsRole>();
        wrapper.orderByDesc("id");
        if (StringUtils.isNotBlank(value)){
            wrapper.like("name",value);
        }
        return this.page(new Page<UmsRole>(pageNo, pageSize),wrapper);
    }

    public Boolean add(String name) {
        UmsRole umsRole = new UmsRole(name,null);
        return this.save(umsRole);
    }

    public Boolean check(String fieldName, String value,Long id) {
        QueryWrapper<UmsRole> wrapper = new QueryWrapper<UmsRole>();
        wrapper.eq(fieldName,value);
        if (id != null){
            //如果进来就证明是修改，必须跟不是我的id的对象的属性不重复
            wrapper.ne("id",id);
        }
        return this.count(wrapper) == 0;
    }

    public Boolean update(Long id,String name) {
        UmsRole umsRole = new UmsRole();
        umsRole.setId(id);
        umsRole.setName(name);
        return this.updateById(umsRole);
    }

    public Boolean del(Long id, Integer active) {
        UmsRole umsRole = new UmsRole();
        umsRole.setId(id);
        umsRole.setActive(active);
        return this.updateById(umsRole);
    }
}
